package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.approve.model.Apl_notepad;
import com.power.sifa.approve.model.pageInfo.Apl_notepadPageInfo;
import com.power.sifa.approve.api.IApl_notepadFeign;
import com.power.sifa.approve.service.Apl_notepadService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   行政复议记事本信息控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_notepadController",tags={"行政复议记事本信息"})
public class Apl_notepadController implements IApl_notepadFeign{

    @Resource
    private Apl_notepadService apl_notepadService;

    
    @Override
    @ApiOperation("查询行政复议记事本信息分页列表")
    @SaCheckPermission("apl_notepad_list")
    public ResponseDTO list(Apl_notepadPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        PageDataResponse page = this.apl_notepadService.findAll(pageInfo);
        return ResponseDTO.succData(page);
    }

    @Override
    @ApiOperation("查看行政复议记事本信息")
    @SaCheckPermission("apl_notepad_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_notepad apl_notepad  = apl_notepadService.getById(id);
        if (apl_notepad != null) {
            apl_notepad.geneText();
        }
        return ResponseDTO.succData(apl_notepad);
    }

    @Override
    @ApiOperation("applyid查看行政复议记事本信息")
    @SaCheckPermission("apl_notepad_viewByApplyid")
    public ResponseDTO viewByApplyid(String applyid) {
        Assert.notNull(applyid, "applyid不能为空");
        List<Apl_notepad> apl_notepad  = apl_notepadService.getByApplyId(applyid);
        return ResponseDTO.succData(apl_notepad);
    }


    @Override
    @ApiOperation("删除行政复议记事本信息")
    @SaCheckPermission("apl_notepad_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_notepadService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改行政复议记事本信息")
    @SaCheckPermission("apl_notepad_save")
    public ResponseDTO save(Apl_notepad apl_notepad) {
        Assert.notNull(apl_notepad, "apl_notepad不能为空");
        apl_notepadService.saveOrUpdate(apl_notepad);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除行政复议记事本信息")
    @SaCheckPermission("apl_notepad_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_notepadService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_notepad_list")
    public void output(HttpServletResponse response, Apl_notepadPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "行政复议记事本信息";
        String[] heads = {"记事本id","记事标题","记事的详细内容","记事的创建人","记事创建日期时间","记事更新的日期时间","创建人id","案号","applyid"};
        String[] fields = {"id","title","content","author","createdate","updatedate","userid","acceptcode","applyid"};
        PageDataResponse page = this.apl_notepadService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getRecords(),response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "行政复议记事本信息";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"记事本id","记事标题","记事的详细内容","记事的创建人","记事创建日期时间","记事更新的日期时间","创建人id","案号","applyid"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","title","content","author","createdate","updatedate","userid","acceptcode","applyid"};
        List<Apl_notepad> list = (List<Apl_notepad>)ExcelUtil.Import(fields, file, Apl_notepad.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_notepadService.saveAll(list);
        }
        return ResponseDTO.succ();
	}
    
}
